﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserAuthForm : Form
    {
        private bool addflag = false;
        public UserAuthForm()
        {
            InitializeComponent();
        }

        private void UserAuth_Load(object sender, EventArgs e)
        {
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
  

            try
            {
                var query = from c in dbcontext.tb_user where c.Role != "系统管理员" 
                           select new { c.ID, c.Name, c.Role};

                this.dataGridView1.DataSource = query;

                this.dataGridView1.Columns[0].HeaderText = "用户号码";
                this.dataGridView1.Columns[1].HeaderText = "用户姓名";
                this.dataGridView1.Columns[2].HeaderText = "用户权限";

                this.dataGridView1.Columns[0].ReadOnly = true;
                this.dataGridView1.Columns[1].ReadOnly = true;
                this.dataGridView1.Columns[2].ReadOnly = true;


            }
            catch (Exception ex)
            {
                MessageBox.Show("不能获取用户列表");
            }

            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

              
            int r = e.RowIndex;
            string id = this.dataGridView1.Rows[r].Cells[0].Value.ToString();

            this.textBox1.Text = id;
            this.textBox2.Text = this.dataGridView1.Rows[r].Cells[1].Value.ToString();

            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            try
            {
                var query = from c in dbcontext.tb_userauth
                            where c.userID == id
                            select c;


                if (query.Count() == 0)
                {
                    return;
                }

                this.checkBox1.Checked = query.First().a == 1 ? true : false;
                this.checkBox2.Checked = query.First().b == 1 ? true : false;
                this.checkBox3.Checked = query.First().c == 1 ? true : false;
                this.checkBox4.Checked = query.First().d == 1 ? true : false;
                this.checkBox5.Checked = query.First().e == 1 ? true : false;
            }

            catch (Exception ex)
            {
                MessageBox.Show("不能获取用户权限");
            }


        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("请选择用户号码！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (addflag == false)
            {
                return;
            }

             int a = this.checkBox1.Checked ? 1 : 0;
             int b = this.checkBox2.Checked ? 1 : 0;
             int g = this.checkBox3.Checked ? 1 : 0;
             int d = this.checkBox4.Checked ? 1 : 0;
             int f = this.checkBox5.Checked ? 1 : 0;

             MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
             string []dest = new string[5];


             tb_userauth item = dbcontext.tb_userauth.First(c => c.userID == this.textBox1.Text);

             if (item == null)
             {
                 return;
             }

             item.a = a;
             item.b = b;
             item.c = g;
             item.d = d;
             item.e = f;

             dest[0] = a == 0 ? "禁止" : "允许";
             dest[1] = b == 0 ? "禁止" : "允许";
             dest[2] = g == 0 ? "禁止" : "允许";
             dest[3] = d == 0 ? "禁止" : "允许";
             dest[4] = f == 0 ? "禁止" : "允许";

             string detail = UserLog.builddetailstring(UserLog.userauthlogheader, "添加", dest, null);
                 
             try
             {
                 dbcontext.SubmitChanges();
                 addflag = false;
                 UserLog.adduserauthlog(User.UserID, User.Username, this.textBox1.Text, this.textBox2.Text,"成功", detail);
                 MessageBox.Show("修改成功");
                 
             }

             catch (Exception ex)
             {
                 UserLog.adduserauthlog(User.UserID, User.Username, this.textBox1.Text, this.textBox2.Text, "失败", detail);
                 MessageBox.Show("修改失败");

             }

        }

        private void UserAuthForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (addflag == false)
            {
                e.Cancel = false;
                return;
            }
            DialogResult r = MessageBox.Show("保存修改？", "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                 button2_Click(sender, e);

                 if (addflag == false)
                 {
                     e.Cancel = false;
                 }
            }
            else if (r == DialogResult.Cancel)
            {
                e.Cancel = true;

            }
        }

        private void checkBox1_Click(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox2_Click(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox3_Click(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox4_Click(object sender, EventArgs e)
        {
            addflag = true;
        }

        private void checkBox5_Click(object sender, EventArgs e)
        {
            addflag = true;
        }




    }
}
